package com.youan.serve.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youan.serve.controller.swagger.LogControllerSwagger;
import com.youan.serve.service.ILogService;
import com.youan.serve.dao.domain.Log;
import com.youan.pangu.base.util.PrimaryKeyUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;




import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto_code
 * @since 2020-03-12
 */
@Controller
@RequestMapping("/log")
public class LogController implements LogControllerSwagger{
    @Autowired
    private ILogService logService;

    @Override
    @PostMapping("/log")
    public Log addLog(Log log){
        log.setLogId(PrimaryKeyUtil.getCommonId());
        logService.save(log);
        return log;
    }

    @Override
    @DeleteMapping("/log/{logId}")
    public void deleteLogByLogId(@PathVariable String logId){
        logService.removeById(logId);
    }

    @Override
    @GetMapping("/log/{logId}")
    public Log selectLogByLogId(@PathVariable String logId){
        return logService.getById(logId);
    }

    @Override
    @PutMapping("/log")
    public Log updateLog(Log log){
        logService.updateById(log);
        return log;
    }

    @Override
    @GetMapping("/logs")
    public List<Log> selectAllLog(){
        return logService.list();
    }

    @Override
    @GetMapping("/logs/example")
    public List<Log> selectLogsByExample(Log log) {
        QueryWrapper<Log> wrapper = new QueryWrapper<>(log);
        return logService.list(wrapper);
    }

    @Override
    @GetMapping("/logs/pageable")
    public Page<Log> selectLogPageable(Page page){
        return logService.page(page);
    }

    @Override
    @GetMapping("/logs/example/pageable")
    public Page<Log> selectLogsByExamplePageable(Page<Log> page, Log log) {
        QueryWrapper<Log> wrapper = new QueryWrapper<>(log);
        return logService.page(page, wrapper);
    }
}

